package com.wmada.spring.controller;

import com.wmada.springdemo.bean.Greeting;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.UUID;

/**
 * @author will.ma
 * @date 2020-9-25
 */
@RestController
@Validated
public class GreetingController {

    private static final String template = "Hello, %s!";


    @Value("${com.wmada.system-name}")
    private String systemName;
    @Value("${com.wmada.system-version}")
    private String systemVersion;

    @RequestMapping("/greeting")
    public Greeting greeting(@NotNull String name, HttpSession session) {
        UUID uid = (UUID) session.getAttribute("uid");
        if (uid == null) {
            uid = UUID.randomUUID();
            session.setAttribute("uid", uid);
        }
        return new Greeting(uid, String.format(template, name), systemName + " v" + systemVersion);
    }
}
